#!/usr/bin/env node
// 脚手架工作过程
// 1.通过命令行交互询问用户信息
// 2.根据用户的回答生成文件
const inquirer = require("inquirer");
const fs = require("fs");
const path = require("path");
const ejs = require("ejs");
const { cwd } = require("process");
inquirer
  .prompt([
    {
      type: "input",
      name: "name",
      message: "请输入项目名"
    },
    {
      type: "input",
      name: "title",
      message: "请输入title"
    }
  ])
  .then(answers => {
    console.log(answers, cwd());
    fs.readdir("./templates", (err, files) => {
      if (err) throw files;
      files.forEach(file => {
        console.log(file);
        ejs.renderFile(
          path.join(__dirname + "/templates", file),
          answers,
          (err, result) => {
            if (err) throw err;
            fs.writeFileSync(
              path.resolve(__dirname, `${answers.name}.${file.split(".")[1]}`),
              result
            );
          }
        );
      });
    });
  });
